﻿//
// File:    Mp3LibraryModelBase.cs
// Project: MP3 Art Embedder v0.2.x
//

using System;
using System.Collections.Generic;
using System.IO;

namespace Mp3LibraryViewed
{
    public interface IAppView
    {
        bool Question (string message, string caption);
    }


    [Flags]
    public enum UpdateMode
    { Default=0, WhatIf=1, ReplaceExisting=2 }

    [Flags]
    public enum FileAction
    { IgnoredReadonly=1, IgnoredNo23Tag=2, IgnoredCantOpen=4, Added=8, Replaced=16 }

    public enum Strength { Normal, Sticky }


    public delegate void ActionLoggedEventHandler (string message, Strength strength);
    public delegate void RunningChangedEventHandler (bool isRunning);
    public delegate void IsVerboseChangedEventHandler ();


    /// <summary>
    /// Define the Modelbase sub-component of the Strict MVC pattern.
    /// This class provides change notifications and a minimal, read-only interface of the model to the view.
    /// </summary>
    public class Mp3LibraryModelBase
    {
        static readonly protected string[] helpText =
        {
            "This program updates MP3 files with cover art found in the nearest image",
            "file encountered in the MP3 file's hierarchy.  The hierarchy will not be",
            "searched any higher than the specified folder.  The cover art will be",
            "reduced to the specified size if it is larger.  If multiple image files are",
            "specified by separating them with semi-colons, the first enountered will",
            "be embedded.  Check the what-if box to see potential changes without",
            "making them.  Results will be shown in this window.",
            "",
            "Limitations:",
            "- Currently works only on MP3 files with v2.3/8859-1 tags.",
            "- Read-only files will not be modified.",
            ""
        };

        protected IAppView view;
        protected bool isVerbose = true;
        protected int maxPicSize = 200;
        protected List<string> artFileNames;

        public void SetView (IAppView view)
        {
            if (this.view != null)
                throw new InvalidOperationException ("View already assigned");
            this.view = view;
        }

        public int CountSkips { get; protected set; }
        public int CountAdds { get; protected set; }
        public int CountReplaces { get; protected set; }
        public int CountErrors { get; protected set; }
        public bool IsVerbose { get { return isVerbose; } }
        public UpdateMode Mode { get; protected set; }
        public long PicQuality { get; private set; }
        public string RootName { get; protected set; }


        public string GetArtFileNames ()
        {
            return string.Join (Path.PathSeparator.ToString(), artFileNames);
        }


        public int MaxPicSize
        {
            get { return maxPicSize; }
            protected set
            {
                if (value < 20)
                    throw new ArgumentException ("Value out of range", "MaxPicSize");
                maxPicSize = value;
            }
        }


        public event ActionLoggedEventHandler ActionLogged;
        public event RunningChangedEventHandler RunningChanged;
        public event IsVerboseChangedEventHandler IsVerboseChanged;


        protected Mp3LibraryModelBase ()
        {
            PicQuality = 95L;
            Mode = UpdateMode.Default;
            artFileNames = new List<string>();
            artFileNames.Add ("folder.jpg");
            artFileNames.Add ("Front.jpg");
        }


        public string GetTotals ()
        {
            return ((Mode & UpdateMode.WhatIf) != 0? "[What-if] " : "")
                + "---- "
                + CountAdds + " added covers, "
                + CountReplaces + " replaced covers, "
                + CountSkips + " untouched files, "
                + CountErrors + " errors ----";
        }


        public void OnActionLogged (string message, Strength strength)
        {
            if (ActionLogged != null)
                ActionLogged (message, strength);
        }


        public void OnRunningChanged (bool isRunning)
        {
            if (RunningChanged != null)
                RunningChanged (isRunning);
        }


        public void OnIsVerboseChanged ()
        {
            if (IsVerboseChanged != null)
                IsVerboseChanged();
        }
    }
}
